package com.ultrawise.survey.common.utils;

import java.io.*;

import com.ultrawise.survey.common.Systemconfig;

public class FileUtils {
	public FileUtils() {
	}

	/**
	 * 删除文件
	 * 
	 * @param strClassID
	 *            String
	 * @param strFileName
	 *            String
	 */
	public static void del(String strClassID, String strFileName) {
		File file = null;
		String strFilePath = "";
		try {
			if (strFileName != null && !strFileName.equals("")) {
				strFilePath = Systemconfig.getProperty("uploadfiles.path")
						+ "/" + strClassID + "/";
				String[] arrFile = strFileName.split(",");
				for (int i = 0; i < arrFile.length; i++) {
					file = new File(strFilePath + strFileName);
					if (file.isFile()) {
						file.delete();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param strFilePathAndName
	 *            String
	 */
	public static void del(String strFilePathAndName) {
		File file = null;
		try {
			if (strFilePathAndName != null && !strFilePathAndName.equals("")) {
				file = new File(strFilePathAndName);
				if (file.isFile()) {
					file.delete();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param oldPathAndName
	 *            String
	 * @param newPathAndName
	 *            String
	 */
	public static void copy(String oldPathAndName, String newPathAndName) {
		try {
			int byteSum = 0;
			int byteRead = 0;
			File oldFile = new File(oldPathAndName);
			if (oldFile.exists()) { // 文件存在时
				InputStream inStream = new FileInputStream(oldPathAndName); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPathAndName);
				byte[] buffer = new byte[1444];
				while ((byteRead = inStream.read(buffer)) != -1) {
					byteSum += byteRead; // 字节数 文件大小
					fs.write(buffer, 0, byteRead);
				}
				inStream.close();
			}
		} catch (Exception e) {
			System.out.println("复制文件操作出错");
			e.printStackTrace();
		}
	}

	/**
	 * 创建文件
	 * 
	 * @param strFile
	 *            String
	 * @param strContent
	 *            String
	 * @return boolean
	 */
	public static boolean createFile(String strFile, String strContent) {
		boolean bResult = true;
		File file = null;
		FileOutputStream fos = null;
		try {
			file = new File(strFile);
			if (file.exists())
				file.delete();
			file.createNewFile();
			fos = new FileOutputStream(file);
			fos.write(strContent.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			file = null;
			fos = null;
		}
		return bResult;
	}

	/**
	 * 复制文件夹
	 * 
	 * @param oldPath
	 *            String
	 * @param newPath
	 *            String
	 * @return boolean
	 */
	public static boolean copyFolder(String oldPath, String newPath) {
		boolean bResult = true;
		try {
			(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator))
					temp = new File(oldPath + file[i]);
				else
					temp = new File(oldPath + File.separator + file[i]);
				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);

					FileOutputStream output = new FileOutputStream(newPath
							+ File.separator + (temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if (temp.isDirectory()) { // 如果是子文件夹
					copyFolder(oldPath + File.separator + file[i], newPath
							+ File.separator + file[i]);
				}
			}
		} catch (Exception e) {
			bResult = false;
			e.printStackTrace();
		}
		return bResult;
	}

	/**
	 * 判断文件夹是否存在
	 * 
	 * @param strPath
	 *            String
	 * @param strFolder
	 *            String
	 * @return boolean
	 */
	public static boolean checkFolder(String strPath, String strFolder) {
		boolean bResult = false;
		try {
			File a = new File(strPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (strPath.endsWith(File.separator))
					temp = new File(strPath + file[i]);
				else
					temp = new File(strPath + File.separator + file[i]);
				if (temp.isDirectory()) { // 如果是子文件夹
					if (file[i].equals(strFolder))
						bResult = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bResult;
	}

	/**
	 * 移动文件
	 * 
	 * @param oldPathAndName
	 *            String
	 * @param newPathAndName
	 *            String
	 */
	public static void move(String oldPathAndName, String newPathAndName) {
		copy(oldPathAndName, newPathAndName);
		del(oldPathAndName);
	}

	/**
	 * 取文件扩展名
	 * 
	 * @param strFileName
	 *            String
	 * @return String
	 */
	public static String getExt(String strFileName) {
		String strExt = "";
		if (strFileName != null && !strFileName.equals("")) {
			strExt = strFileName.substring(strFileName.lastIndexOf(".") + 1,
					strFileName.length());
		}
		return strExt;
	}

	/**
	 * 删除文件夹
	 * 
	 * @param folderPath
	 *            String
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除指定文件夹下所有文件
	 * 
	 * @param path
	 *            String
	 * @return boolean
	 */
	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists())
			return flag;
		if (!file.isDirectory())
			return flag;
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator))
				temp = new File(path + tempList[i]);
			else
				temp = new File(path + File.separator + tempList[i]);
			if (temp.isFile())
				temp.delete();
			if (temp.isDirectory()) {
				delAllFile(path + File.separator + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + File.separator + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}
}
